package com.crape.message.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crape.account.bo.IAccountBO;
import com.crape.common.bean.LoginUser;
import com.crape.common.enums.MessageType;
import com.crape.common.enums.ResultType;
import com.crape.common.model.Account;
import com.crape.common.model.MsgUser;
import com.crape.common.thread.ThreadObjManager;
import com.crape.common.utils.PageUtils;
import com.crape.message.bo.IMsgUserBO;

@Controller
public class MessageController {

	private IMsgUserBO msgUserBOImpl;
	private IAccountBO accountBOImpl;

	@Autowired
	public void setMsgUserBOImpl(IMsgUserBO msgUserBOImpl) {
		this.msgUserBOImpl = msgUserBOImpl;
	}

	@Autowired
	public void setAccountBOImpl(IAccountBO accountBOImpl) {
		this.accountBOImpl = accountBOImpl;
	}

	@RequestMapping("/message/write.jsp")
	public String write(ModelMap modelMap, String username, String message) {
		if (username == null) {
			modelMap.put("message", "联系人不能为空！");
			return "/alert.html";
		}
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		if (username.equalsIgnoreCase(loginUser.getUsername())) {
			modelMap.put("message", "联系人不能为自己！");
			return "/alert.html";
		}
		if (message == null) {
			modelMap.put("message", "内容不能为空！");
			return "/alert.html";
		}
		Account account = accountBOImpl.getAccountByUsername(username.trim());
		if (account == null) {
			modelMap.put("message", "联系人不存在！");
			return "/alert.html";
		}
		Long receiveid = account.getId();
		Long sendid = loginUser.getAccountid();
		MsgUser msg = new MsgUser();
		msg.setSendid(sendid);
		msg.setReceiveid(receiveid);
		msg.setMessage(message);
		msgUserBOImpl.sendMsg(msg);
		return "redirect:/message/write.html";
	}

	@RequestMapping("/message/reply.json")
	public ModelAndView reply(Long receiveid, String message) {
		ModelAndView mv = new ModelAndView();
		if (receiveid == null) {
			mv.addObject("flag", 0);
			mv.addObject("message", "联系人不能为空！");
			return mv;
		}
		if (message == null) {
			mv.addObject("flag", 0);
			mv.addObject("message", "内容不能为空！");
			return mv;
		}
		Account account = accountBOImpl.getAccountById(receiveid);
		if (account == null) {
			mv.addObject("flag", 0);
			mv.addObject("message", "联系人不存在！");
			return mv;
		}
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		Long sendid = loginUser.getAccountid();
		MsgUser msg = new MsgUser();
		msg.setSendid(sendid);
		msg.setReceiveid(receiveid);
		msg.setMessage(message);
		msgUserBOImpl.sendMsg(msg);
		mv.addObject("flag", ResultType.SUCCESS.getCode());
		mv.addObject("message", "发送成功！");
		return mv;
	}

	@RequestMapping("/message/lettersInbox.jsp")
	public String lettersInbox(ModelMap modelMap, Integer pageNo) {
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		Long receiveid = loginUser.getAccountid();
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		Long count = msgUserBOImpl.countReceivedMsgs(receiveid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<MsgUser> letters = msgUserBOImpl.getPagedReceivedMsgs(receiveid, startIndex, pageSize);
			modelMap.put("letters", letters);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/message/lettersInbox.html";
	}

	@RequestMapping("/message/lettersOutbox.jsp")
	public String lettersOutbox(ModelMap modelMap, Integer pageNo) {
		if (pageNo == null || pageNo == 0) {
			pageNo = 1;
		}
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		Long sendid = loginUser.getAccountid();
		Long count = msgUserBOImpl.countSendedMsgs(sendid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<MsgUser> letters = msgUserBOImpl.getPagedMsgsSended(sendid, startIndex, pageSize);
			modelMap.put("letters", letters);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		return "/message/lettersOutbox.html";
	}

	@RequestMapping("/message/letterInfo.jsp")
	public String letterInfo(ModelMap modelMap, Long receiveid, Long sendid, Integer pageNo) {
		if (receiveid == null) {
			return "redirect:/message/write.html";
		}
		Account user = accountBOImpl.getAccountById(receiveid);
		if (user == null) {
			modelMap.put("message", "联系人不存在！");
			return "/alert.html";
		}
		modelMap.put("receiveid", receiveid);
		modelMap.put("sendid", sendid);
		modelMap.put("user", user);
		Long count = msgUserBOImpl.countMsgInfos(receiveid, sendid);
		modelMap.put("count", count == null ? 0 : count);
		modelMap.put("pageNo", pageNo);
		if (count != null && count > 0) {
			Integer pageSize = 20;
			Integer startIndex = PageUtils.getStartIndex(pageNo, pageSize);
			List<MsgUser> letters = msgUserBOImpl.getPagedMsgInfos(receiveid, sendid, startIndex, pageSize);
			modelMap.put("letters", letters);
			modelMap.put("pageNumArray", PageUtils.getPagedNumArray(count, pageSize));
		}
		LoginUser loginUser = ThreadObjManager.getLoginUser();
		Long accountid = loginUser.getAccountid();
		if(accountid.compareTo(receiveid)==0){
			modelMap.put("letterType", MessageType.receive.getCode());
			modelMap.put("yourId", sendid);
		}else if(accountid.compareTo(sendid)==0){
			modelMap.put("letterType", MessageType.send.getCode());
			modelMap.put("yourId", receiveid);
		}
		return "/message/letterInfo.html";
	}

	@RequestMapping("/message/notifys.jsp")
	public String notifys(ModelMap modelMap) {
		return "/message/notifys.html";
	}

	@RequestMapping("/message/notifyInfo.jsp")
	public String infoNotifys(ModelMap modelMap) {
		return "/message/notifyInfo.html";
	}

}
